package control.table
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import events.TableEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import global.GameState;
	import global.Resource;
	
	import model.room.TableModel;
	
	import utils.DisplayTool;
	
	import view.components.ImgButton;

	public class TableControl
	{
		private var sitButs:Array;
		private var tableModel:TableModel;
		private var _view:View = View.getInstance();
		private var _model:Model = Model.getInstance();
		private var _control:Control = Control.getInstance();
		
		public function TableControl(tableModel:TableModel)
		{
			this.tableModel = tableModel;
			initSitButs();
			initListeners();
		}
		
		private function initListeners():void
		{
			tableModel.addEventListener(TableEvent.SHOW_CARDS,onShowCards);
			tableModel.addEventListener(TableEvent.BANKER_CHANGE,onBankerChange);
			tableModel.addEventListener(TableEvent.SYSTEM_MSG,onSystemMsg);
			tableModel.addEventListener(TableEvent.REMAIN_MULTI,onRemainMulti);
			tableModel.addEventListener(TableEvent.CLEAR_ROUND_CARDS,onClearRoundCards);
			tableModel.addEventListener(TableEvent.CHAT_MSG,onChatMsg);
			tableModel.addEventListener(TableEvent.TABLE_ANIMATION,onTableAnimation);
			
			//mail
			
		}
		
		private function onTableAnimation(event:TableEvent):void
		{
			var effect:MovieClip = Resource.getTableAnimation(event.effectId);
			effect.x = GameState.GAME_WIDTH / 2;
			effect.y = GameState.GAME_HEIGHT / 2 - 100;
			_view.tableLayer.addEmote(effect);
			effect.gotoAndPlay(1);
			effect.addEventListener(Event.COMPLETE,onAnimationComplete);
		}
		
		private function onAnimationComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onAnimationComplete);
			DisplayTool.removeChild(event.target.parent as DisplayObjectContainer, event.target as DisplayObject);
		}
		
		public function onStandUp():void
		{
			_model.netRemote.sendStandUp();
		}	
		
		private function onChatMsg(event:TableEvent):void
		{
			_view.playPanel.updateChatMsg(event.msgContent);
		}
		
		private function onClearRoundCards(event:TableEvent):void
		{
			_view.tableLayer.clearShowCards();
		}
		
		private function onRemainMulti(event:TableEvent):void
		{
			_view.tableLayer.updateMultiRemain(event.remain,event.multi);
		}
		
		private function onSystemMsg(event:TableEvent):void
		{
			_view.playPanel.updateServerMsg(event.msgContent);
		}
		
		private function onBankerChange(event:TableEvent):void
		{
			var bankerSitId:int = event.newBankerSitId;
			_view.tableLayer.updateBanker(bankerSitId);
		}
		
		private function onShowCards(event:TableEvent):void
		{
			_view.tableLayer.showCards(event.showCards);	
		}
		
		private function initSitButs():void
		{
			sitButs = [];
			var imgBut:ImgButton;
			for (var i:int=0; i<6; i++)
			{
				imgBut = new ImgButton(GameState.sitButXYS[i][0],GameState.sitButXYS[i][1],i,onSitButClick,-1,-1,"tableBut",null,"坐下");
				sitButs.push(imgBut);
				_view.tableLayer.addSitBut(imgBut);
			}	
		}
		
		private function onSitButClick(event:MouseEvent):void
		{
			_model.netRemote.sendSitDown(event.target.id);
		}
		
		public function showGiveCardAnimate(sitId:int):void
		{
			_view.tableLayer.playGiveCardAnimate(sitId);
		}
		
		//信息按钮 
		public function mailButClick():void
		{
			_view.tableLayer.showHideMailBut(false);
		}
		
		public function backToHall():void
		{
			tableModel.gotoHall();
		}
		
		
	}
}